package zhuzx.solution;

import java.io.File;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.StandardOpenOption;
import java.util.List;

public class Truncator {
	
	public static void main(String[] args) throws IOException {
//		Path path = Paths.get("C:\\Users\\zhuzx\\Desktop\\JdbcUtil.java");
		deleteByDir("D:\\Java\\huoli\\rc-util\\src\\main\\java\\com\\huoli\\rc\\util");
	}
	
	static void deleteByDir(String dirPath) throws IOException {
		File dir = new File(dirPath);
		File[] files = dir.listFiles();
		for (File file : files) {
			delete(file.toPath());
		}
	}
	
	static void delete(Path path) throws IOException {
		List<String> list = Files.readAllLines(path, StandardCharsets.UTF_8);
		StringBuilder sb = new StringBuilder();
		for (String line : list) {
			sb.append(deleteParamsAndReturn(line) + "\n");
		}
		Files.write(path, sb.toString().getBytes(StandardCharsets.UTF_8), StandardOpenOption.TRUNCATE_EXISTING);
	}
	
	static String deleteParamsAndReturn(String line) {
		String[] matches = {"\t * @param", "     * @param", "\t * @return", "     * @return"};
		for (String prefix : matches) {
			if (line.startsWith(prefix)) {
				if (prefix.charAt(prefix.length()-1) == 'm') {
					return line.substring(0, line.indexOf(' ', prefix.length()+2));
				} else {
					return prefix;
				}
			}
		}
		return line;
	}

	static String deleteAll(String line) {
		if (line.matches("^\\s*/?\\*.*")) {
			return "";
		} else {
			return line;
		}
	}
	
}